//
//  JXWBuyWebViewController.m
//  JXWShopping4Ipad
//
//  Created by zheng ji on 14-1-18.
//  Copyright (c) 2014年 jxw. All rights reserved.
//

#import "JXWBuyWebViewController.h"

 
@interface JXWBuyWebViewController ()
@property (nonatomic, strong, readonly) UIBarButtonItem *backBarButtonItem;
@property (nonatomic, strong, readonly) UIBarButtonItem *forwardBarButtonItem;
@property (nonatomic, strong, readonly) UIBarButtonItem *refreshBarButtonItem;
@property (nonatomic, strong, readonly) UIBarButtonItem *stopBarButtonItem;


- (void)updateToolbarItems;

- (void)goBackClicked:(UIBarButtonItem *)sender;
- (void)goForwardClicked:(UIBarButtonItem *)sender;
- (void)reloadClicked:(UIBarButtonItem *)sender;
- (void)stopClicked:(UIBarButtonItem *)sender;
@end

@implementation JXWBuyWebViewController

 @synthesize backBarButtonItem, forwardBarButtonItem, refreshBarButtonItem, stopBarButtonItem  ;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
-(void)viewDidDisappear:(BOOL)animated{
    _webView.delegate=nil;
    _webView=nil;
    [super viewDidDisappear:animated];
}
- (void)goBackClicked:(UIBarButtonItem *)sender {
    [_webView goBack];
}

- (void)goForwardClicked:(UIBarButtonItem *)sender {
    [_webView goForward];
}

- (void)reloadClicked:(UIBarButtonItem *)sender {
    [_webView reload];
}

- (void)stopClicked:(UIBarButtonItem *)sender {
    [_webView stopLoading];
    
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
	[self updateToolbarItems];
}
- (void)closeButtonClicked:(UIBarButtonItem *)sender {
    //[self dismissViewControllerAnimated:YES completion:nil];
    [self.parentPopverController dismissPopoverAnimated:YES ];
    //[self.parentPopverController release];
}
 
- (UIBarButtonItem *)backBarButtonItem {
    
    if (!backBarButtonItem) {
        backBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStylePlain target:self action:@selector(goBackClicked:)];
        backBarButtonItem.imageInsets = UIEdgeInsetsMake(2.0f, 0.0f, -2.0f, 0.0f);
		//backBarButtonItem.width = 18.0f;
    }
    return backBarButtonItem;
}

- (UIBarButtonItem *)forwardBarButtonItem {
    
    if (!forwardBarButtonItem) {
        forwardBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"forward"] style:UIBarButtonItemStylePlain target:self action:@selector(goForwardClicked:)];
        forwardBarButtonItem.imageInsets = UIEdgeInsetsMake(2.0f, 0.0f, -2.0f, 0.0f);
		//forwardBarButtonItem.width = 18.0f;
    }
    return forwardBarButtonItem;
}

- (UIBarButtonItem *)refreshBarButtonItem {
    
    if (!refreshBarButtonItem) {
        refreshBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(reloadClicked:)];
    }
    
    return refreshBarButtonItem;
}





//创建工具条
-(void) updateToolbarItems{
 
    UIBarButtonItem *refreshStopBarButtonItem = _webView.isLoading ? self.stopBarButtonItem : self.refreshBarButtonItem;
    
    UIBarButtonItem *fixedSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    fixedSpace.width = 1.0f;
 
    
    NSArray *items=[NSArray arrayWithObjects:
                    fixedSpace,
                   self.backBarButtonItem,
                    fixedSpace,
                   self.forwardBarButtonItem,
                    fixedSpace,
                  refreshStopBarButtonItem,
                   nil];
    
    self.navigationItem.rightBarButtonItems=items;
    
    UIBarButtonItem* closeButtonItem=[[UIBarButtonItem alloc] initWithTitle:@"关闭" style:UIBarButtonItemStylePlain target:self action:@selector(closeButtonClicked:)];
    
 
    
    self.navigationItem.leftBarButtonItem=closeButtonItem;
    
    
}

- (void)viewDidLoad
{
    [super viewDidLoad];
  
        
   
    self.navigationItem.title=@"商品详情";
    [self updateToolbarItems];
    
    CGFloat subHeight=0;
    UIInterfaceOrientation t_orientation=[[UIApplication sharedApplication] statusBarOrientation ];
    if ( UIInterfaceOrientationIsLandscape(t_orientation)) {
        subHeight=200;//横屏
    }
 
    CGRect webFrame=self.view.frame;
       _webView=[[UIWebView alloc]initWithFrame:CGRectMake(webFrame.origin.x, webFrame.origin.y, webFrame.size.width  , webFrame.size.height-subHeight)];
    _webView.opaque=NO;
    _webView.scalesPageToFit=YES;
 
    
    _webView.delegate=self;
    [self.view addSubview:_webView];
    
    _activityIndicatorView=[[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(20.0f, 20.0f, 32.0f, 32.0f)];
    [_activityIndicatorView setCenter:self.view.center];
    [_activityIndicatorView setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];
    _activityIndicatorView.center=_webView.center;
	
    
    [self.view addSubview:_activityIndicatorView];
   
    [self loadWebUrl];
    //
    
}
-(void)loadWebUrl{
    
    if(self.webURL !=nil && _webView !=nil){
        
        
        
        [_webView loadRequest:[[NSURLRequest alloc]
                                   initWithURL:[NSURL URLWithString:self.webURL]]];
                
    }
}
-(void) webViewDidStartLoad:(UIWebView *)webView{
    UIApplication* uiApp=[UIApplication sharedApplication];
    uiApp.networkActivityIndicatorVisible=YES;
    [_activityIndicatorView startAnimating];
    [self updateToolbarItems];
}
-(void)webViewDidFinishLoad:(UIWebView *)webView{
    
    CGSize contentSize = webView.scrollView.contentSize;
    CGSize viewSize = self.view.bounds.size;
    
    float scale = viewSize.width / contentSize.width;
    if (scale < 0.9) {
        NSLog(@"Zoom out fix for web view: %f", scale);
        
        webView.scrollView.minimumZoomScale = scale;
        webView.scrollView.maximumZoomScale = scale+5;
        webView.scrollView.zoomScale = scale;
    }
    
    [_activityIndicatorView stopAnimating];
    UIApplication* uiApp=[UIApplication sharedApplication];
    
    uiApp.networkActivityIndicatorVisible=NO;
    [self updateToolbarItems];
}
-(void) webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
  /*  UIAlertView* alertView=[[UIAlertView alloc] initWithTitle:@"" message:[error localizedDescription] delegate:nil  cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [alertView show];
    [alertView release];
    [self updateToolbarItems];*/
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
   // [MobClick beginLogPageView:@"BuyView"];
   // [MobClick beginEvent:@"productBuy" primarykey:@"productBuy" attributes:[NSDictionary dictionaryWithObject:@"webUrl" forKey:self.webURL]];
    
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    //[MobClick endLogPageView:@"BuyView"];
    //[MobClick endEvent:@"productBuy" primarykey:@"productBuy"];
}
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    _webView.scalesPageToFit=YES;
    return YES;
}
-(void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
    
    NSLog(@"");
    
}
-(void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
    NSLog(@"");

}

 
@end
